package com.woniu40.petai_manager.web;

import com.woniu40.petai_manager.entity.*;
import com.woniu40.petai_manager.result.ResponseResult;
import com.woniu40.petai_manager.service.ReportService;
import com.woniu40.petai_manager.vo.PermissionVo;
import com.woniu40.petai_manager.vo.ReportCommentVo;
import com.woniu40.petai_manager.vo.ReportDynamicVo;
import com.woniu40.petai_manager.vo.RoleVo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import java.sql.Timestamp;
import java.util.*;

@RestController
@RequestMapping("report")
@CrossOrigin
public class ReportController {

    @Autowired
    ReportService reportService;

    @GetMapping("/dyn")
    @ApiOperation("获得所有动态举报")
    public ResponseResult<List<ReportDynamicVo>> DynReport() {
        //        未处理的内容
        List<Report> reports = reportService.findDyn(0);
        //        已经处理的内容
        List<Report> finishReports = reportService.findDyn(1);

        Map<Integer, Dynamic> dynamics = new HashMap<Integer,Dynamic>();
        for (Report r: reports){
            Integer dynamicId = r.getDynamicId();
            Dynamic dynamic = reportService.findByDynId(dynamicId);
            dynamics.put(dynamicId,dynamic);
        }

        List<ReportDynamicVo>dynamicVoList=new ArrayList<>();
        for (Dynamic d:dynamics.values()){
            Integer count = 0;
            List<Report>reportList=new ArrayList<>();
            Integer dynamicId = d.getDynamicId();
            for (Report r:reports){
                if (r.getDynamicId()==dynamicId){
                    reportList.add(r);
                    count++;
                }
            }
            for (Report r:finishReports){
                if (r.getDynamicId()==dynamicId){
                    count++;
                }
            }
            ReportDynamicVo vo=new ReportDynamicVo(d.getDynamicId(),d.getDynamicTitle(),d.getCreateTime(),d.getUserId(),d.getViewNumber(),d.getStatus(),d.getDynamicCover(),d.getPlateId(),d.getDynamicPriority(),d.getContentText(),reportList,count);
            dynamicVoList.add(vo);
        }

        Comparator<ReportDynamicVo> c1 = new Comparator<ReportDynamicVo>() {
            @Override
            public int compare(ReportDynamicVo o1, ReportDynamicVo o2) {
                return o2.getCount()-o1.getCount();
            }
        };
        dynamicVoList.sort(c1);
        return new ResponseResult<List<ReportDynamicVo>>(200, "传输成功", dynamicVoList);
    }

    @GetMapping("/com")
    @ApiOperation("获取所有评论举报")
    public ResponseResult<List<ReportCommentVo>> ComReport() {

        //        未处理的内容
        List<Report> reports = reportService.findCom(0);
        //        已经处理的内容
        List<Report> finishReports = reportService.findCom(1);

        Map<Integer, Comment> comments = new HashMap<Integer,Comment>();
        for (Report r: reports){
            Integer commentId = r.getCommentId();
            Comment comment = reportService.findByComId(commentId);
            comments.put(commentId,comment);
        }
        List<ReportCommentVo>commentVoList=new ArrayList<>();
        for (Comment c:comments.values()){
            Integer count = 0;
            List<Report>reportList=new ArrayList<>();
            Integer commentId = c.getCommentId();
            for (Report r:reports){
                if (r.getCommentId()==commentId){
                    reportList.add(r);
                    count++;
                }
            }
            for (Report r:finishReports){
                if (r.getCommentId()==commentId){
                    count++;
                }
            }
            ReportCommentVo vo=new ReportCommentVo(c.getCommentId(),c.getUserId(),c.getDynamicId(),c.getCreateTime(),c.getParentId(),c.getParentUserId(),c.getCommentStatus(),c.getCommentContent(),reportList,count);
            commentVoList.add(vo);
        }

        Comparator<ReportCommentVo> c1 = new Comparator<ReportCommentVo>() {
            @Override
            public int compare(ReportCommentVo o1, ReportCommentVo o2) {
                return o2.getCount()-o1.getCount();
            }
        };
        commentVoList.sort(c1);
        return new ResponseResult<List<ReportCommentVo>>(200, "传输成功", commentVoList);
    }

    @GetMapping("/comLate")
    @ApiOperation(value = "获取延期未处理的评论")
    public ResponseResult<List<ReportCommentVo>> ComLateReport() {
        //获取24小时前的时间
        Calendar cal=Calendar.getInstance();
        cal.setTime(new Date());
        cal.set(Calendar.HOUR_OF_DAY,cal.get(Calendar.HOUR_OF_DAY)-24);
        Date date=cal.getTime();
        Timestamp timestamp = new Timestamp(date.getTime());

        //  获得未处理的延期评论集合
        List<Report> reports = reportService.findComLate(timestamp);

        Map<Integer, Comment> comments = new HashMap<Integer,Comment>();

        for (Report r: reports){
            Integer commentId = r.getCommentId();
            Comment comment = reportService.findByComId(commentId);
            comments.put(commentId,comment);
        }
        List<ReportCommentVo>commentVoList=new ArrayList<>();
        for (Comment c:comments.values()){
            Integer count=0;
            List<Report>reportList=new ArrayList<>();
            Integer commentId = c.getCommentId();
            for (Report r:reports){
                if (r.getCommentId()==commentId){
                    reportList.add(r);
                    count++;
                }
            }
            ReportCommentVo vo=new ReportCommentVo(c.getCommentId(),c.getUserId(),c.getDynamicId(),c.getCreateTime(),c.getParentId(),c.getParentUserId(),c.getCommentStatus(),c.getCommentContent(),reportList,count);
            commentVoList.add(vo);
        }

        Comparator<ReportCommentVo> c1 = new Comparator<ReportCommentVo>() {
            @Override
            public int compare(ReportCommentVo o1, ReportCommentVo o2) {
                return o2.getCount()-o1.getCount();
            }
        };
        commentVoList.sort(c1);
        return new ResponseResult<List<ReportCommentVo>>(200, "传输成功", commentVoList);
    }

    @GetMapping("/dynLate")
    @ApiOperation(value = "获取延期未处理的动态")
    public ResponseResult<List<ReportDynamicVo>> DynLateReport() {
        //获取24小时前的时间
        Calendar cal=Calendar.getInstance();
        cal.setTime(new Date());
        cal.set(Calendar.HOUR_OF_DAY,cal.get(Calendar.HOUR_OF_DAY)-24);
        Date date=cal.getTime();
        Timestamp timestamp = new Timestamp(date.getTime());
        //未处理的内容
        List<Report> reports = reportService.findDynLate(timestamp);
        Map<Integer, Dynamic> dynamics = new HashMap<Integer,Dynamic>();
        for (Report r: reports){
            Integer dynamicId = r.getDynamicId();
            Dynamic dynamic = reportService.findByDynId(dynamicId);
            dynamics.put(dynamicId,dynamic);
        }

        List<ReportDynamicVo>dynamicVoList=new ArrayList<>();
        for (Dynamic d:dynamics.values()){
            Integer count = 0;
            List<Report>reportList=new ArrayList<>();
            Integer dynamicId = d.getDynamicId();
            for (Report r:reports){
                if (r.getDynamicId()==dynamicId){
                    reportList.add(r);
                    count++;
                }
            }
            ReportDynamicVo vo=new ReportDynamicVo(d.getDynamicId(),d.getDynamicTitle(),d.getCreateTime(),d.getUserId(),d.getViewNumber(),d.getStatus(),d.getDynamicCover(),d.getPlateId(),d.getDynamicPriority(),d.getContentText(),reportList,count);
            dynamicVoList.add(vo);
        }

        Comparator<ReportDynamicVo> c1 = new Comparator<ReportDynamicVo>() {
            @Override
            public int compare(ReportDynamicVo o1, ReportDynamicVo o2) {
                return o2.getCount()-o1.getCount();
            }
        };
        dynamicVoList.sort(c1);
        return new ResponseResult<List<ReportDynamicVo>>(200, "传输成功", dynamicVoList);
    }

    @RequestMapping("/stateHandle")
    @ResponseBody
    @ApiOperation(value = "修改举报状态")
    @ApiImplicitParam(name="reportId",value = "举报id",dataType = "int",paramType = "query",required = false)
    public ResponseResult changeState(@RequestBody Map map) {
        Integer reportId = (Integer) map.get("reportId");
        int i = reportService.updateState(reportId);
        if (i == 1) {
            return new ResponseResult(200,"处理完毕");
        } else {
            return new ResponseResult(110,"处理失败");
        }
    }

    @PostMapping("/dynHandle")
    @ResponseBody
    @ApiOperation(value = "批量修改动态举报状态")
    @ApiImplicitParam(name="dynamicId",value = "动态id",dataType = "int",paramType = "query",required = false)
    public ResponseResult changeDynState(@RequestBody Map map) {
        Integer dynamicId = (Integer) map.get("dynamicId");
        Integer reportId = (Integer) map.get("reportId");
        int i = reportService.updataDyn(dynamicId);
        if (i == 1) {
            return new ResponseResult(200,"处理完毕");
        } else {
            return new ResponseResult(110,"处理失败");
        }
    }
    @PostMapping("/comHandle")
    @ResponseBody
    @ApiOperation(value = "批量修改评论举报状态")
    @ApiImplicitParam(name="commentId",value = "评论id",dataType = "int",paramType = "query",required = false)
    public ResponseResult changeComState(@RequestBody Map map) {
        Integer commentId = (Integer) map.get("commentId");

        int i = reportService.updataCom(commentId);
        if (i == 1) {
            return new ResponseResult(200,"处理完毕");
        } else {
            return new ResponseResult(110,"处理失败");
        }
    }
}
